//
//  HXFoodMaterialVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/15.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXFoodMaterialVC.h"
#import "HX_Macro.h"
#import "UIImage+HXImageColor.h"
#import "HXRelatedFoodVC.h"
#import "HXPurchaseTipsVC.h"
#import "HXNutritionalEffectVC.h"
#import "HXPracticalEncyclopediaVC.h"
#import "HXScrollecViewTCell.h"
#import "HXRelatedFoodModel.h"
#import "HXRelatedBigModel.h"
#import "HXPurchaseTipsModel.h"

@interface HXFoodMaterialVC ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *sectionView;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *viewNav;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, strong) UIViewController *showVC;
@property (nonatomic, strong) NSMutableArray *related;
@property (nonatomic, assign) NSInteger addHeight;
 /**
 *  滚动视图数组源
 */
@property (nonatomic, strong) NSMutableArray *arrAll;
@end

@implementation HXFoodMaterialVC
- (NSMutableArray *)arrAll {
    if (!_arrAll) {
        _arrAll = [NSMutableArray array];
    }
    return _arrAll;
}
- (NSMutableArray *)related {
    if (!_related) {
        _related = [NSMutableArray array];
    }
    return _related;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLORAUTO;
    [self getNetWorkingFoodUserId:self.userId];
    [self addChildVC];
    [self creatHeaderView];
    [self tableView];
    [self creatTitleHeaderNaView];
}
- (void)creatTitleHeaderNaView {
    self.viewNav = [[UIView alloc] init];
    _viewNav.HX_width = SCREEN_WIDTH;
    _viewNav.HX_height = 64;
    _viewNav.HX_x = 0;
    _viewNav.HX_y = 0;
    [self.view addSubview:_viewNav];
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftBtn setImage:[UIImage imageNamed:@"xiangzuoBack1"] forState:UIControlStateNormal];
    _leftBtn.centerY = _viewNav.centerY;
    _leftBtn.HX_x = 20;
    _leftBtn.HX_width = 30;
    _leftBtn.HX_height = 20;
    [self.viewNav addSubview:_leftBtn];
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.HX_x = SCREEN_WIDTH - 50;
    _rightBtn.HX_width = 20;
    _rightBtn.HX_height = 20;
    _rightBtn.centerY = _leftBtn.centerY;
    [_rightBtn setImage:[UIImage imageNamed:@"ico_collection_gray"] forState:UIControlStateNormal];
    [_rightBtn addTarget:self action:@selector(addRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewNav addSubview:_rightBtn];
    [self.leftBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)addRightBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"ico_collection_gray_p"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(addRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(removeRightBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)removeRightBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"ico_collection_gray"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(removeRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(addRightBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)creatTitleLable {
    self.labelTitle = [[UILabel alloc] init];
    self.labelTitle.text = self.titles;
    self.labelTitle.textColor = [UIColor blackColor];
    self.labelTitle.HX_width = [SelfSizeTools selfSizeHeightWithString:_labelTitle.text height:40 Font:[UIFont systemFontOfSize:17]];
     _labelTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.f];
    _labelTitle.alpha = 0;
    self.labelTitle.HX_height = self.viewNav.HX_height;
    self.labelTitle.centerX = self.viewNav.centerX;
    self.labelTitle.centerY = self.leftBtn.centerY;
    [self.viewNav addSubview:_labelTitle];
}
- (void)back:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatHeaderView {
    self.imageHeader = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    _imageHeader.backgroundColor = COLORAUTO;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = _imageHeader;
        [self.view addSubview:_tableView];
        [_tableView registerClass:[HXScrollecViewTCell class] forCellReuseIdentifier:@"scrollView"];
    }
    return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXScrollecViewTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"scrollView"];
    cell.scrollView.delegate = self;
    cell.scrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    self.contentView = cell.scrollView;
    cell.scrollView.pagingEnabled = YES;
    [self scrollViewDidEndScrollingAnimation:cell.scrollView];
    return cell;
}
- (void)addChildVC {
    HXRelatedFoodVC *relatedFood = [[HXRelatedFoodVC alloc] init];
    relatedFood.userId = self.userId;
    HXPurchaseTipsVC *purchaseTips = [[HXPurchaseTipsVC alloc] init];
    HXNutritionalEffectVC *nutritionalEffect = [[HXNutritionalEffectVC alloc] init];
    HXPracticalEncyclopediaVC *practic = [[HXPracticalEncyclopediaVC alloc] init];
    [self addChildViewController:relatedFood];
    [self addChildViewController:purchaseTips];
    [self addChildViewController:nutritionalEffect];
    [self addChildViewController:practic];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        self.sectionView = [[UIView alloc] init];
        _sectionView.backgroundColor = [UIColor redColor];
        NSArray *navTitlte = @[@"相关菜例", @"选购要诀", @"营养功效", @"实用百科"];
        _lineView = [[UIView alloc] init];
        _lineView.HX_height = 2;
        _lineView.backgroundColor = [UIColor redColor];
        for (NSInteger i = 0; i < 4; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:navTitlte[i] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            btn.tag = 1000 + i;
            btn.frame = CGRectMake(SCREEN_WIDTH / 4 * i, 0, SCREEN_WIDTH / 4, 50);
            [_sectionView addSubview:btn];
            [btn addTarget:self action:@selector(addChildBtn:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
            if (i == 0) {
                [self addChildBtn:btn];
                _lineView.HX_width = btn.HX_width;
                _lineView.HX_y = 50 - 2;
            }
        }
        [_sectionView addSubview:_lineView];
        return _sectionView;
   
}
- (void)addChildBtn:(UIButton *)btn {
    self.selectBtn.enabled = YES;
    btn.enabled = NO;
    NSInteger i = btn.tag - 1000;
    [UIView animateWithDuration:0.5 animations:^{
        self.lineView.HX_width = btn.HX_width;
        self.lineView.centerX = btn.centerX;
    }];
    self.selectBtn = btn;
    [self.contentView addSubview:_showVC.view];
    CGFloat index = i * SCREEN_WIDTH;
    [self.contentView setContentOffset:CGPointMake(index, 0) animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (void)getNetWorkingFoodUserId:(NSString *)userId {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"material_id=%@&methodName=MaterialView&version=4.40&", userId];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        self.titles = dicData[@"material_name"];
        [self.imageHeader sd_setImageWithURL:[NSURL URLWithString:dicData[@"image"]]];
        HXPurchaseTipsModel *purchase = [HXPurchaseTipsModel ModelWithDic:dicData];
        [self.arrAll addObject:purchase];
        [self creatHeaderView];
        [self creatTitleLable];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger i = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.showVC = self.childViewControllers[i];
    HXPurchaseTipsModel *purchase = [self.arrAll firstObject];
    if (i == 1) {
        HXPurchaseTipsVC *vc = (HXPurchaseTipsVC *)self.showVC; 
        vc.pickDetail = purchase.pick;
        vc.imgString = purchase.pick_image;
    } else if (i == 2) {
        HXNutritionalEffectVC *nutritionVC = (HXNutritionalEffectVC *)self.showVC;
        nutritionVC.purchaseDetail = purchase.effect;
        nutritionVC.imgString = purchase.effect_image;
    } else if (i == 3) {
        HXPracticalEncyclopediaVC *practicalVC = (HXPracticalEncyclopediaVC *)self.showVC;
        practicalVC.practionDetail = purchase.applied;
        practicalVC.imgString = purchase.applied_image;
    }
    _showVC.view.frame = CGRectMake(scrollView.contentOffset.x, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
    [scrollView addSubview:_showVC.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self addChildBtn:self.sectionView.subviews[index]];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
    CGFloat offset_y = scrollView.contentOffset.y;
    CGFloat alpha = (offset_y) / 150.0f;
    CGFloat colorF = 1 - alpha;
    self.viewNav.backgroundColor = [UIColor colorWithWhite:0.955 alpha:alpha];
    UIImageView *imgLeft = [[UIImageView alloc] init];
    imgLeft.image = [UIImage imageNamed:@"xiangzuoBack1"];
    imgLeft.image = [imgLeft.image imageMaskWithColor:[UIColor colorWithWhite:colorF alpha:1.000]];
    if (scrollView.contentOffset.y <= 50 && scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);

    }else if (scrollView.contentOffset.y > 50) {
        scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    }
    [self.leftBtn setImage:imgLeft.image forState:UIControlStateNormal];
    self.labelTitle.alpha = alpha;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_HEIGHT - 64;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
